import pymysql
import asyncio

db_2m = {
    'host': '101.42.235.191',  # 主机
    'user': 'wy-robot',  # 用户名
    'password': 'HIUz8s645wLoP814Ss',  # 密码
    'port': 3306,  # 端口 3306
    'database': 'wy-robot'  # 数据库名
}


async def main():
    # 连接2m库
    connection_2m = pymysql.connect(**db_2m)
    # 创建游标对象
    cursor_2m = connection_2m.cursor()
    try:
        # 从2m库中选择所有数据
        cursor_2m.execute(f"select ip from app_douyin where create_time >= '2023-12-28 00:00:00';")
        rows = cursor_2m.fetchall()
        for row in rows:
            ip = row[0]
            # print(f"{idfa}")
            sql_select = "select user_id,device from app_user where device like '%"+ip+"%'"
            # print(f"sql = {sql_select}")
            cursor_2m.execute(sql_select)
            users = cursor_2m.fetchall()
            for u in users:
                print(f"user = {u[0]}")
                print(f"{ip}")
                print(f"device = {u[1]}")


    except (pymysql.Error) as error:
        print("导入数据表时发生错误:", error)

    # 关闭游标和连接
    cursor_2m.close()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())